package cn.edu.gues.infosystam.controller;


import cn.edu.gues.infosystam.entity.Cclass;
import cn.edu.gues.infosystam.entity.Student;
import cn.edu.gues.infosystam.entity.Teacher;
import cn.edu.gues.infosystam.entity.User;
import cn.edu.gues.infosystam.service.CclassService;
import cn.edu.gues.infosystam.service.StudentService;
import cn.edu.gues.infosystam.service.TeacherService;
import cn.edu.gues.infosystam.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 苏小小
 * @since 2020-06-11
 */
@Controller
@RequestMapping("/Teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private CclassService cclassService;

    @Autowired
    HttpSession httpSession;

    /**
     * 学生个人信息页面
     *
     * @return
     */
    @RequestMapping("/Teacher")
    public String listTeacherView(@RequestParam(value = "p", defaultValue = "1") Integer p, Model model) {
        Teacher teacher = getUserInfoByTeacher();
        if (teacher == null) {
            return "Login";
        }

        Page<Teacher> teacherList = teacherService.page(new Page<Teacher>(p, 30));

        model.addAttribute("teacherList", teacherList);
        return "Teacher_Teacher";
    }

    @RequestMapping("/Student")
    public String listStudentView(@RequestParam(value = "p", defaultValue = "1") Integer p, Model model) {
        Teacher teacher = getUserInfoByTeacher();
        if (teacher == null) return "Login";

        IPage<Student> students = studentService.getStudents(new Page<Student>(p, 30));

        QueryWrapper<Cclass> queryWrapper = new QueryWrapper<Cclass>();
        queryWrapper.select("DISTINCT college").eq("deleted",0);
        List<Cclass> colleges = cclassService.list(queryWrapper);

        model.addAttribute("studentList", students);
        model.addAttribute("colleges", colleges);

        return "Teacher_Student";
    }

    @PostMapping("Del")
    @ResponseBody
    public R delTeacherById(Integer tid){
        Teacher teacher = getUserInfoByTeacher();
        if (teacher == null) {
            return R.error().message("您尚未登录，请先登录");
        }

        boolean remove = teacherService.removeById(tid);
        if (remove)
            return R.success();
        else
            return R.error().message("删除教师失败");
    }

    @ResponseBody
    @PostMapping("Save")
    public R saveOrUpdate(Teacher teacher){
        Teacher t = getUserInfoByTeacher();
        if (t == null) return R.error().message("您尚未登录，请先登录");

        System.out.println(teacher);
        boolean saveOrUpdate = teacherService.saveOrUpdate(teacher);
        if (saveOrUpdate)
            return R.success();
        else
            return R.error().message("没有保存成功，请检查内容是否正确");
    }


    /**
     * 获取教师登录信息
     * @return
     */
    private Teacher getUserInfoByTeacher(){
        User userInfo = (User)httpSession.getAttribute("userInfo");
        if (userInfo == null) {
            httpSession.setAttribute("errorMsg", "请先登录");
            // 登录后可以返回的界面
            httpSession.setAttribute("historyHref","/Report/List");
            return null;
        }

        return (Teacher) userInfo.getUserInfo();
    }
}



